﻿#ifndef VIDEOCONTROLER_H
#define VIDEOCONTROLER_H
#include "Aconfig.h"

#include "ui_cameraframe.h"

#include <QObject>
#include <QDebug>
#include <QTimer>

#include <omp.h>
#include <chrono>
#include <vector>
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/calib3d.hpp>

class VideoControler : public QObject
{
    Q_OBJECT
public:
    explicit VideoControler(QObject *parent = 0);

    bool getVideoControlerInit_state();

    bool sgbmInit();

    bool open_camera();
    bool close_camera();

    void start_ObstacleDetection();
    void stop_ObstacleDetection();

    void processDisparity();


    void showGreyScaleDepthMap(cv::Mat& disparity_8u);
    void showColorDepthMap(cv::Mat& dis_color);
    cv::Mat createMask(const cv::Size& shape, int topHeight, int bottomHeight);
    cv::Mat applyMask(const cv::Mat &depthImage, const cv::Mat &mask);
    void showColorMap(std::vector<std::vector<cv::Point>>& hull);


    void warningObstacle();




signals:
    void initialized_sig();
    void obstacleDetection_sig(cv::Mat& frame);

    void sendSpeechTeskToController_sig(QString str,int priority);

public slots:
    bool initialize();
    void captureFrame_slot();  // 定时捕获帧
    void obstacleDetection_slot(cv::Mat& frame);



private:
    bool initialized_state = false;

private:
    QWidget* cameraFrameWidget;
    Ui::CameraFrame_wg*  cameraFrameUi;

private:
    cv::VideoCapture capture;
    QTimer* timer;

private:
    cv::Mat leftCameraMatrix,rightCameraMatrix;
    cv::Mat leftDistortion,rightDistortion;
    cv::Mat R,T;
    cv::Mat R1, R2, P1, P2, Q;
    cv::Mat left_map1, left_map2, right_map1, right_map2;
    cv::Ptr<cv::StereoSGBM> stereo = cv::StereoSGBM::create();
    cv::Size imageSize;
    cv::Mat disparity;
    cv::Mat currentFrame;

    std::map<std::string, int> counters;
    std::map<std::string, int> lastObstacle;
    std::map<std::string, int> currentObstacle;
    std::map<std::string, int> counters_withoutObstacle = {{"left", 0}, {"front", 0}, {"right", 0}};
    const int THRESHOLD = 5;
    const int DECAY = 4;
    const int WITHOUTOBSTACLEFRAME = 30;



};

#endif // VIDEOCONTROLER_H
